<?php

require_once('functions/pageLoad.php');
loginRequired(true);
$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];

$sql = "SELECT jobs.id, jobs.user_id, jobs.area_id, jobs.trade_id, area, trade, addresses.longitude, addresses.latitude FROM jobs 
	LEFT JOIN addresses ON jobs.address_id = addresses.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	WHERE jobs.id = '".mysql_real_escape_string($_POST['jid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$job_rows = mysql_num_rows($query);

if($job_rows > 0){

$job_rs=mysql_fetch_assoc($query);

// approved tradesman?
//Change By Vyas Ishan 24 May 2013 Task2 added the status field condition
$sql = "SELECT tradesman.id, radius, longitude, latitude FROM tradesman 
		LEFT JOIN subscriptions ON tradesman.subscription_id = subscriptions.id 
		LEFT JOIN addresses ON tradesman.address_id = addresses.id 
		LEFT JOIN tradesman_to_trade ON tradesman.id = tradesman_to_trade.tradesman_id 
		WHERE tradesman.user_id = '".$user_session[0]."' AND tradesman_to_trade.trade_id = '".$job_rs['trade_id']."' AND subscriptions.status = '1' AND tradesman.active = 1";

$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$rows = mysql_num_rows($query);
$tradesman_rs=mysql_fetch_assoc($query);

$tradesman_id = $tradesman_rs['id'];
$approved_tradesman = ($rows > 0) ? 1 : 0;
$tradesman_radius = $tradesman_rs['radius'];

if($approved_tradesman == 1){
$in_tradesmans_area = (distance_calc($job_rs['latitude'], $job_rs['longitude'], $tradesman_rs['latitude'], $tradesman_rs['longitude'], 'm') <= $tradesman_rs['radius']) ? true : false ;
}

if($approved_tradesman == 1 && $in_tradesmans_area){

$sql = "SELECT id FROM quotes WHERE job_id = '".mysql_real_escape_string($_POST['jid'])."' AND tradesman_id = '".$tradesman_id."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$tradesman_quoted=(mysql_num_rows($query) > 0) ? 1 : 0 ;

if($tradesman_quoted != 1){

$sql = "INSERT INTO quotes (tradesman_id, job_id, quote, materials, info, date_quoted) VALUES ('".$tradesman_id."', '".$job_rs['id']."', '".mysql_real_escape_string(stripslashes(str_replace(",","",$_POST['quote'])))."', '".mysql_real_escape_string($_POST['materials'])."', '".mysql_real_escape_string(stripslashes($_POST['additional_info']))."', NOW())";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

$job_link = build_job_link($job_rs['trade'],$job_rs['area'],$job_rs['id']);

// send email to user

$sql = "SELECT email, firstname, surname, mobile, mobile_verified, new_quotation, quotations FROM users WHERE id = '".$job_rs['user_id']."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$user_rows = mysql_num_rows($query);
$user_rs=mysql_fetch_assoc($query);

if($user_rs['quotations'] == '1'){

require_once("classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = "mail@iwantatradesman.co.uk";
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($user_rs['email']);

	$mail->Subject = "Quote notification";
	$mail->Body = "Dear ".$user_rs['firstname']." ".$user_rs['surname']."\r\n\r\nA tradesman has submitted a quote for your job at I Want A Tradesman.\r\n\r\nYou can choose to accept the quote here: http://www.iwantatradesman.co.uk".$job_link."\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

}

// send sms?

$sql = "SELECT sms_remaining FROM SMS WHERE user_id = '".$job_rs['user_id']."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$sms_rs=mysql_fetch_assoc($query);

if($sms_rs['sms_remaining'] > 0 && $user_rs['mobile_verified'] == '1' && $user_rs['new_quotation'] == '1'){

// SEND SMS

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['sms_email'];
	$mail->FromName = $settings['sms_email'];

	$mail->AddAddress("44".substr($user_rs['mobile'], 1)."@bulksms.co.uk");

	$mail->Subject = $settings['sms_password'];
	$mail->Body = "A tradesman has sent you a quotation for your job on I Want A Tradesman. Go to your account now to view the details.";

	$mail->Send();


// deduct sms

$new_sms_remaining = $sms_rs['sms_remaining'] - 1;

$sql = "UPDATE SMS SET sms_remaining = '".$new_sms_remaining."' WHERE user_id = '".$job_rs['user_id']."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

}

header("Location: ".$job_link."?result=quote");

}

}

}

?>